package test.sansungtest2;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity implements SensorEventListener {

	SensorManager sensorManager;
	Sensor acelerometerSensor;
	Sensor compassSensor;
	Sensor gyroscopeSensor;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		
		
		acelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		compassSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
		gyroscopeSensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
	}
	
	public void sensorChanged(SensorEvent event){
		
		
		TextView txtXAcceletometer = (TextView) findViewById(R.id.txtXAccelerometerx);
		TextView txtYAcceletometer = (TextView) findViewById(R.id.txtYAccelerometerx);
		TextView txtZAcceletometer = (TextView) findViewById(R.id.txtZAccelerometerx);
		
		TextView txtXGyroscope = (TextView) findViewById(R.id.txtXGyroscopex);
		TextView txtYGyroscope = (TextView) findViewById(R.id.txtYGyroscopex);
		TextView txtZGyroscope = (TextView) findViewById(R.id.txtZGyroscopex);
		
		TextView txtXCompass = (TextView) findViewById(R.id.txtXCompassx);
		TextView txtYCompass = (TextView) findViewById(R.id.txtYCompassx);
		TextView txtZCompass = (TextView) findViewById(R.id.txtZCompassx);
		
		
		Sensor defaultSensor = sensorManager.getDefaultSensor(event.sensor.getType());
		
		if(defaultSensor.getType() == Sensor.TYPE_ACCELEROMETER){
			txtXAcceletometer.setText(String.valueOf(event.values[0]));
			txtYAcceletometer.setText(String.valueOf(event.values[1]));
			txtZAcceletometer.setText(String.valueOf(event.values[2]));
		}else{
			if(defaultSensor.getType() == Sensor.TYPE_GYROSCOPE){
				txtXGyroscope.setText(String.valueOf(event.values[0]));
				txtYGyroscope.setText(String.valueOf(event.values[1]));
				txtZGyroscope.setText(String.valueOf(event.values[2]));
			}else{
				if(defaultSensor.getType() == Sensor.TYPE_ROTATION_VECTOR){
					txtXCompass.setText(String.valueOf(event.values[0]));
					txtYCompass.setText(String.valueOf(event.values[1]));
					txtZCompass.setText(String.valueOf(event.values[2]));
				}
			}
		}
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			return rootView;
		}
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		sensorChanged(event);
		
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
	}
	
	
	@Override
	protected void onResume() {
		super.onResume();
		sensorManager.registerListener(this, acelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
		sensorManager.registerListener(this, compassSensor, SensorManager.SENSOR_DELAY_NORMAL);
		sensorManager.registerListener(this, gyroscopeSensor, SensorManager.SENSOR_DELAY_NORMAL);
		
	}

}
